package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.dto.CallBlockRuleDTO;
import com.xbongbong.paas.pojo.dto.CallConfigInfoDTO;
import com.xbongbong.paas.pojo.dto.CallSetEffectiveTimeDTO;
import com.xbongbong.paas.pojo.dto.CallSwitchAutoOpenDetailDTO;
import com.xbongbong.paas.pojo.dto.CallSwitchAutoOpenDisturbDTO;
import com.xbongbong.paas.pojo.dto.CallSwitchBatchCallDTO;
import com.xbongbong.paas.pojo.vo.CallBlockRuleVO;
import com.xbongbong.paas.pojo.vo.CallCenterConfigVO;
import com.xbongbong.paas.pojo.vo.CallSetEffectiveTimeVO;
import com.xbongbong.paas.pojo.vo.CallSwitchAutoOpenDetailVO;
import com.xbongbong.paas.pojo.vo.CallSwitchAutoOpenDisturbVO;
import com.xbongbong.paas.pojo.vo.CallSwitchBatchCallVO;
import com.xbongbong.paas.service.CallCenterConfigService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author youli.chen
 * @version v1.0
 * @description: 话务中心 设置项 API 控制器
 * @date 2019/2/22 15:08
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/callCenter/config")
public class CallCenterConfigController {

    @Resource
    private CallCenterConfigService callCenterConfigService;

    /**
     * 获取当前公司的呼叫中心设置
     *
     * @return 获取当前公司的呼叫中心设置
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/info", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String info(@RequestBody @Valid CallConfigInfoDTO callConfigInfoDTO, BindingResult br) {
        XbbResponse<CallCenterConfigVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterConfigService.getByCorpId(callConfigInfoDTO.getCorpid(), callConfigInfoDTO.getModuleType());
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 开关自动打开详情开关(来电是否自动弹出客户详情) 显隐号码
     *
     * @return 开关自动打开详情开关(来电是否自动弹出客户详情)) 显隐号码
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/switchAutoOpenDetail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String switchAutoOpenDetail(@RequestBody @Valid CallSwitchAutoOpenDetailDTO callSwitchAutoOpenDetailDTO, BindingResult br) {
        XbbResponse<CallSwitchAutoOpenDetailVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterConfigService.switchAutoOpenDetail(callSwitchAutoOpenDetailDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 开启批量外呼（主要是销帮帮呼叫中心）
     *
     * @return 开启批量外呼（主要是销帮帮呼叫中心）
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/switchBatchCall", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String switchBatchCall(@RequestBody @Valid CallSwitchBatchCallDTO callSwitchBatchCallDTO, BindingResult br) {
        XbbResponse<CallSwitchBatchCallVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterConfigService.switchBatchCall(callSwitchBatchCallDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 开启防骚扰策略（主要是销帮帮呼叫中心）
     *
     * @return 开启防骚扰策略（主要是销帮帮呼叫中心）
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/switchAutoOpenDisturb", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String switchAutoOpenDisturb(@RequestBody @Valid CallSwitchAutoOpenDisturbDTO callSwitchAutoOpenDisturbDTO, BindingResult br) {
        XbbResponse<CallSwitchAutoOpenDisturbVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterConfigService.switchAutoOpenDisturb(callSwitchAutoOpenDisturbDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 开启防封号规则（主要是销帮帮呼叫中心）
     *
     * @return 开启防封号规则（主要是销帮帮呼叫中心）
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/switchBlockRule", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String switchBlockRule(@RequestBody @Valid CallBlockRuleDTO callBlockRuleDTO, BindingResult br) {
        XbbResponse<CallBlockRuleVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterConfigService.switchBlockRule(callBlockRuleDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 自定义设置有效通话时长（主要是销帮帮呼叫中心）为null时赋值0
     *
     * @return 自定义设置有效通话时长（主要是销帮帮呼叫中心）为null时赋值0
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/setEffectiveTime", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String setEffectiveTime(@RequestBody @Valid CallSetEffectiveTimeDTO callSetEffectiveTimeDTO, BindingResult br) {
        XbbResponse<CallSetEffectiveTimeVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterConfigService.setEffectiveTime(callSetEffectiveTimeDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


}
